package com.yixing.tech.system.controller;

import com.yixing.tech.common.base.*;
import com.yixing.tech.common.enums.UserType;
import com.yixing.tech.common.service.CommonService;
import com.yixing.tech.system.service.IPortalService;
import com.yixing.tech.system.service.IUserService;
import com.yixing.tech.common.vo.s3.UserVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

import static com.yixing.tech.common.security.context.UserContext.getTenantId;
import static com.yixing.tech.common.service.CommonService.createToken;

/**
 * @author yixing tech
 * at 2021/12/20
 * describe：
 */
@RestController
@CrossOrigin
@RequestMapping(value = "/user")
public class UserController extends BaseControl {
    @Resource
    IUserService userService;
    @Resource
    IPortalService portalService;
    @Autowired
    private CommonService commonService;

    @PostMapping("/add")
    public Rs<Object> add(@RequestBody UserVO vo) throws BusinessException {
        String tenantId = vo.getTenantId();
         commonService.fillCreateUser(vo);
        if (tenantId != null) {
            vo.setTenantId(tenantId);
        }
        userService.add(vo);
        return Rs.ok();
    }

    @GetMapping("/template")
    public void getImportTemplate(HttpServletResponse response) throws Exception {
        userService.getImportTemplate(response);
    }

    @PostMapping("/import")
    public Rs<Object> importEntity(@RequestParam MultipartFile file) throws Exception {
        userService.importUsers(file, commonService.getApiUser());
        return Rs.ok();
    }

    @PostMapping("/listPage")
    public Rs<Object> list(@RequestBody PageRow<ConditionsVO> vo) throws BusinessException {
        vo.getCondition(ConditionsVO.class).setTenantId(getTenantId());
        Page<UserVO> page = userService.listPage(vo);
        return Rs.ok(page);
    }

    @PostMapping("/list")
    public Rs<Object> list(@RequestBody UserVO vo) throws BusinessException {
        vo.setTenantId(getTenantId());
        List<UserVO> page = userService.list(vo);
        return Rs.ok(page);
    }

    @PostMapping("/update")
    public Rs<Object> update(@RequestBody UserVO userVO) throws BusinessException {
        commonService.fillUpdateUser(userVO);
        userVO.setId(commonService.getApiUser().getId());
        userService.update(userVO);
        return Rs.ok();
    }

    @PostMapping("/modify/password")
    public Rs<Object> modifyPW(@RequestBody UserVO userVO) throws BusinessException {
        userVO.setId(commonService.getApiUser().getId());
        userService.modifyPW(userVO);
        return Rs.ok();
    }

    @GetMapping("/{userId}/reset/password")
    public Rs<Object> resetPW(@PathVariable String userId) throws BusinessException {
        UserVO userVO = new UserVO();
        commonService.fillUpdateUser(userVO);
        userVO.setId(userId);
        userService.resetPW(userVO);
        return Rs.ok();
    }

//    @PostMapping("/wx/update")
//    public Rs<Object> updateWxUser(@RequestBody Map<String, String> map) throws BusinessException {
//        UserVO userVO = userService.updateWxUser(map.get("phoneStr"), map.get("wxName"), map.get("avatarUrl"), commonService.getApiUser().getId());
//        reflashApiUser(userVO.clone1(),getApiToken());
//        return Rs.ok();
//    }

    @GetMapping("/bind/wx")
    public Rs<Object> bindWx(@RequestParam String jsCode) throws BusinessException {
        UserVO userVO = commonService.getApiUser();
        userService.bindWx(jsCode, userVO.getId(), userVO.getTenantId());
        return Rs.ok();
    }

    @PostMapping("/update/{userId}")
    public Rs<Object> updateUser(@RequestBody UserVO userVO, @PathVariable String userId) throws BusinessException {
        //管理员修改其他人
        commonService.fillUpdateUser(userVO);
        userVO.setId(userId);
        userService.update(userVO);
        return Rs.ok();
    }

    @DeleteMapping("/{userId}")
    public Rs<Object> del(@PathVariable String userId) throws BusinessException {
        userService.deleteById(userId);
        return Rs.ok();
    }

    @GetMapping("/{userId}")
    public Rs<Object> get(@PathVariable String userId) throws BusinessException {
        UserVO vo = userService.queryById(userId);
        return Rs.ok(vo);
    }

    @GetMapping("/{userId}/freeze")
    public Rs<Object> freeze(@PathVariable String userId) throws BusinessException {
        userService.freezeOrUnFreeze(userId, true);
        return Rs.ok();
    }

    @GetMapping("/{userId}/unfreeze")
    public Rs<Object> unfreeze(@PathVariable String userId) throws BusinessException {
        userService.freezeOrUnFreeze(userId, false);
        return Rs.ok();
    }

    @GetMapping("/{userId}/login")
    public Rs<Object> doLogin(@PathVariable String userId) throws BusinessException {
        UserVO vo = portalService.downLogin(userId, commonService.getApiUser());
        String token = createToken();
        vo.setToken(token);
        commonService.reflashApiUser(vo.clone1(), token, UserType.平台用户.name());
        return Rs.ok(vo);
    }

}
